/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mcx.exception.EntityExistException;
import com.mcx.modules.domain.Account;
import com.mcx.modules.repository.AccountRepository;
import com.mcx.modules.service.AccountService;
import com.mcx.modules.service.dto.AccountDto;
import com.mcx.modules.service.dto.AccountQueryCriteria;
import com.mcx.modules.service.mapstruct.AccountMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AccountServiceImpl implements AccountService {

    private final AccountRepository accountRepository;
    private final AccountMapper accountMapper;

    @Override
    public Page<Account> queryAll(AccountQueryCriteria criteria, Pageable pageable) {
        Page<Account> page = accountRepository.findAll(pageable);
        return page;
    }

    @Override
    public List<AccountDto> queryAll(AccountQueryCriteria criteria){
        return accountMapper.toDto(accountRepository.findAll());
    }

    @Override
    @Transactional
    public AccountDto findById(Integer id) {
        return accountMapper.toDto(accountRepository.findById(id).orElseGet(Account::new));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AccountDto create(Account resources) {
        return accountMapper.toDto(accountRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Account resources) {
        Account account = accountRepository.findById(resources.getId()).orElseGet(Account::new);
        account.copy(resources);
        accountRepository.save(account);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            accountRepository.deleteById(id);
        }
    }

    @Override
    public Account register(Account resources) {
        Account account = accountRepository.findByUsername(resources.getUsername());
        if (account == null) {
            // 说明用户不存在,可以注册
            // 初始化余额
            resources.setYuanbao(0);
            // 获得当前时间
            resources.setCreatetime(Timestamp.valueOf(LocalDateTime.now()));
            return accountRepository.save(resources);
        }
        throw new EntityExistException(Account.class, "username", resources.getUsername());
    }

    @Override
    public Account login(JSONObject jsonData) {
        Account account = accountRepository.findByUsernameAndPwd(jsonData.get("UserName").toString(), jsonData.get("Pwd").toString());
        if (account != null) return account;
        throw new EntityExistException(Account.class, "username", "账号或密码错误");
    }
}